//
//  DaraViewController.m
//  WowDara
//
//  Created by Siroj Chongkolwanont on 5/4/56 BE.
//  Copyright (c) 2556 Siroj Chongkolwanont. All rights reserved.
//

#import "DaraViewController.h"
#import "DaraDetailViewController.h"
#import "Dara.h"

@interface DaraViewController ()

@end

@implementation DaraViewController

@synthesize daraImage;
@synthesize daraLabel;
@synthesize dara;
@synthesize detailText;
@synthesize detailWebText;
@synthesize detailView;
@synthesize bookCollectionCell;
@synthesize profileBtn;
@synthesize magazineBtn;
@synthesize tvBtn;


NSString* details;

NSInteger mode;

NSMutableArray *magazineArray;
NSMutableArray *tvArray;


id jsonObjectFromUrlString(NSString *urlString);

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
  
    if(mode ==1)
        return magazineArray.count;
    else
        return tvArray.count;
  
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ContentCell" forIndexPath:indexPath];
  
    NSInteger row = indexPath.row;
    
    //News* hilight = hilightArray[row];
    
    Dara* dara_obj;
    
    if(mode ==1)
        dara_obj= magazineArray[row];
    else
        dara_obj= tvArray[row];
    
    NSString *url_picture = @"http://119.59.127.95/~wowdara/";
    
    url_picture = [url_picture stringByAppendingString:dara_obj.image_url];
    
    NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:url_picture]];
    
    UIImageView *recipeImageView = (UIImageView *)[cell viewWithTag:100];
    
    recipeImageView.image = [UIImage imageWithData:imageData];
    
    UILabel *recipeNameLabel = (UILabel *)[cell viewWithTag:101];
    recipeNameLabel.text = dara_obj.name;
    
    return cell;
}

- (void)load_dara
{
    magazineArray = [[NSMutableArray alloc] init];
    tvArray  = [[NSMutableArray alloc] init];
    
    
    NSString *dara_str = [NSString stringWithFormat:@"http://119.59.127.95/~wowdara/android/getDaraDetail.php?id=%@", dara.id];
    
    NSString *mag_str = [NSString stringWithFormat:@"http://119.59.127.95/~wowdara/android/getMagazines.php?id=%@", dara.id];
    
    NSString *tv_str = [NSString stringWithFormat:@"http://119.59.127.95/~wowdara/android/getTV.php?id=%@", dara.id];
    
    NSDictionary *publicDara =  jsonObjectFromUrlString(dara_str);
    NSDictionary *publicMag =  jsonObjectFromUrlString(mag_str);
    NSDictionary *publicTv =  jsonObjectFromUrlString(tv_str);
    
    
    
    for(NSString *item in [publicDara allKeys]) {
        
        if([item isEqual: @"detail"] )
        {
            details = [publicDara valueForKey:item];
            
        }
    }
    
    
    for(NSString *item in [publicMag allKeys]) {
        
        if([item isEqual: @"tvmagazines"] )
        {
            NSArray *allBanner = [publicMag valueForKey:item];
            
            for(NSDictionary *hilight in allBanner )
            {
                Dara* feed_obj = [[Dara alloc] init];
                feed_obj.id = [hilight valueForKey:@"id"];
                feed_obj.name = [hilight valueForKey:@"name"];
                feed_obj.image_url = [hilight valueForKey:@"images_url"];
               
                [magazineArray addObject:feed_obj];
            }
            
        }
    }
    
    for(NSString *item in [publicTv allKeys]) {
        
        if([item isEqual: @"tvmagazines"] )
        {
            NSArray *allBanner = [publicTv valueForKey:item];
            
            for(NSDictionary *hilight in allBanner )
            {
                Dara* feed_obj = [[Dara alloc] init];
                feed_obj.id = [hilight valueForKey:@"id"];
                feed_obj.name = [hilight valueForKey:@"name"];
                feed_obj.image_url = [hilight valueForKey:@"images_url"];
                
                [tvArray addObject:feed_obj];
            }
            
        }
    }
    
    [detailWebText loadHTMLString:[NSString stringWithFormat:@"<html><body text=\"#FFFFFF\">%@</body></html>", details] baseURL:nil];
    
}

- (void)viewDidLoad
{
    
    mode = 1;
    self.navigationItem.titleView = [[UIImageView alloc] initWithImage:[UIImage  imageNamed:@"toolbar_logo.png"]];
    
    UIButton *button1 = [[UIButton alloc] init];
    button1.frame= CGRectMake(0,0,43,32);
    [button1 setBackgroundImage:[UIImage imageNamed: @"nav_back.png"] forState:UIControlStateNormal];
    [button1 addTarget:self action:@selector(backAction:) forControlEvents:UIControlEventTouchUpInside];
    
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:button1];
    
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    daraLabel.text = dara.name;
    
    NSString *url_picture = @"http://119.59.127.95/~wowdara/";
    
    url_picture = [url_picture stringByAppendingString:dara.image_url];
    
    NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:url_picture]];
    
    [daraImage setImage:[UIImage imageWithData:imageData]];
    
    [self load_dara];
    
    detailView.hidden = false;
    bookCollectionCell.hidden = true;
    
    
    
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)profile_click:(id)sender {
    [profileBtn setBackgroundImage:[UIImage imageNamed:@"sc_3active.png"] forState:UIControlStateNormal];
    [magazineBtn setBackgroundImage:[UIImage imageNamed:@"sc_4.png"] forState:UIControlStateNormal];
    [tvBtn setBackgroundImage:[UIImage imageNamed:@"sc_5.png"] forState:UIControlStateNormal];
    
    detailView.hidden = false;
    bookCollectionCell.hidden = true;
    
}
- (IBAction)magazine_click:(id)sender {
    [profileBtn setBackgroundImage:[UIImage imageNamed:@"sc_3.png"] forState:UIControlStateNormal];
    [magazineBtn setBackgroundImage:[UIImage imageNamed:@"sc_4active.png"] forState:UIControlStateNormal];
    [tvBtn setBackgroundImage:[UIImage imageNamed:@"sc_5.png"] forState:UIControlStateNormal];
    
    detailView.hidden = true;
    bookCollectionCell.hidden = false;
    
    mode = 1;
    
    [bookCollectionCell reloadData];
}
- (IBAction)tv_click:(id)sender {
    [profileBtn setBackgroundImage:[UIImage imageNamed:@"sc_3.png"] forState:UIControlStateNormal];
    [magazineBtn setBackgroundImage:[UIImage imageNamed:@"sc_4.png"] forState:UIControlStateNormal];
    [tvBtn setBackgroundImage:[UIImage imageNamed:@"sc_5active.png"] forState:UIControlStateNormal];
    
    detailView.hidden = true;
    bookCollectionCell.hidden = false;
    mode = 2 ;
    [bookCollectionCell reloadData];
}

- (IBAction)backAction:(id)sender {
    
    [self.navigationController popViewControllerAnimated:YES];
}
@end
